package com.gao.controller;

import com.gao.Dao.EmployeeDao;
import com.gao.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * @Projectname SSM
 * @Filename EmployeeController
 * @Author an
 * @Data 2022/8/5 14:46
 * @Description
 *
 * 查询所有员工的信息--->/employee--->GET
 * 跳转到添加页面--->/to/add--->GET
 * 新增员工的信息--->/employee--->POST
 * 跳转到修改页面--->/employee/1--->GET
 * 修改员工的信息--->/employee--->PUT
 * 删除员工的信息--->/employee/1--->DELETE
 *
 */
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeDao employeeDao;


    @GetMapping("/employee")
    public String getAllEmployee(Model model){
        //获取所有的员工信息
        Collection<Employee> all = employeeDao.getAll();
        //将所有员工信息在请求域中共享
        model.addAttribute("allEmployee",all);
        return "employee_list";
    }

    @PostMapping("/employee")
    public String addEmployee(Employee employee){
        //保存员工信息
        employeeDao.save(employee);

        return "redirect:/employee";
    }

    @GetMapping("/employee/{id}")
    public String toUpdate(@PathVariable("id") Integer id,Model model){
        //根据id查询员工信息
        Employee employee = employeeDao.get(id);
        //将员工信息共享到请求域中
        model.addAttribute("employee",employee);
        //跳转到employee_update.html
        return "employee_update";
    }

    @PutMapping("/employee")
    public String updateEmployee(Employee employee){
        //修改员工信息
        employeeDao.save(employee);
        return "redirect:/employee";
    }

    @DeleteMapping("/employee/{id}")
    public String deleteEmployee(@PathVariable("id") Integer id){
        //删除员工信息
        employeeDao.delete(id);
        //重定向到列表
        return "redirect:/employee";
    }
}
